import { createRouter, createWebHistory } from 'vue-router'

import { useUserStore } from '@/stores/index'

import routes from './routes'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
})

router.beforeEach((to) => {
  document.title = `在线问诊-${to.meta.title || '页面没有title'}`
  const store = useUserStore()
  if (!store.user.token && !['/login'].includes(to.path)) {
    return '/login'
  }
})

export default router
